package com.vicene.gmail.cart.controller;

import com.vicene.gmail.cart.interceptor.CartInterceptor;
import com.vicene.gmail.cart.service.CartService;
import com.vicene.gmail.cart.vo.Cart;
import com.vicene.gmail.cart.vo.CartItem;
import com.vicene.gmail.cart.vo.UserInfoTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {
    @Autowired
    private CartService cartService;

    @ResponseBody
    @GetMapping("/currentUserCartItems")
    public List<CartItem> getCurrentUserCartItems()  {
        //UserInfoTo userInfoTo = CartInterceptor.threadLocal.get();
        //System.out.println(userInfoTo);

        return cartService.getUserCartItems();
    }


    @GetMapping("/cart.html")
    public String cartListPage(HttpSession httpSession , Model model) throws ExecutionException, InterruptedException {
        //UserInfoTo userInfoTo = CartInterceptor.threadLocal.get();
        //System.out.println(userInfoTo);
        Cart cart = cartService.getCart();
        model.addAttribute("cart" , cart);
        return "cartList";
    }

    @GetMapping("/addToCart")
    public String addToCart(HttpSession httpSession, @RequestParam("skuId")Long skuId ,
                            @RequestParam("num")Long num , RedirectAttributes ra) throws ExecutionException, InterruptedException {
        CartItem cartItem = cartService.addToCart(skuId , num);
        ra.addAttribute("skuId" , skuId);
        // model.addAttribute("skuId" , skuId);
        return "redirect:http://cart.gmail.com/addToCartSuccess.html";
    }

    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccessPage( @RequestParam("skuId")Long skuId , Model model) throws ExecutionException, InterruptedException {
        CartItem item = cartService.getCartItem(skuId);

        model.addAttribute("item" , item);
        return "success";
    }

    @GetMapping("/checkItem")
    public String checkItem( @RequestParam("skuId")Long skuId ,  @RequestParam("check")Integer check,Model model) throws ExecutionException, InterruptedException {
        cartService.checkItem(skuId ,check );
        return "redirect:http://cart.gmail.com/cart.html";
    }

    @GetMapping("/countItem")
    public String countItem( @RequestParam("skuId")Long skuId ,  @RequestParam("num")Integer num,Model model) throws ExecutionException, InterruptedException {
        cartService.changeItemCount(skuId ,num );
        return "redirect:http://cart.gmail.com/cart.html";
    }


    @GetMapping("/deleteItem")
    public String deleteItem( @RequestParam("skuId")Long skuId ) throws ExecutionException, InterruptedException {
        cartService.deleteItem(skuId );
        return "redirect:http://cart.gmail.com/cart.html";
    }

}
